package taller2.model.graph.algorithms;

import java.util.ArrayList;

import taller2.interfaces.AlgorithmApplicator;
import taller2.interfaces.Graph;

public abstract class AlgorithmApplicatorImpl implements AlgorithmApplicator{
	protected ArrayList<String> graphPaths;
	protected ArrayList<Integer> pseudocodeLines;
	protected ArrayList<String> auxText; //para guardar texto extra a mostrar en cada paso
	protected int position;
	
	public AlgorithmApplicatorImpl(){
		reset();
	}
	// advances to the next step of the algorithm
	public void NextStep(){
		if(!noPseudocodeLines() && (position < pseudocodeLines.size()-1))
			position++;
	}
	
	// goes back to the first step of the algorithm
	public void firstStep(){
		position = 0;
	}

	// goes back to the previous step of the algorithm
	public void previousStep(){
		if(position > 0)
			position--;
	}

	// returns the path to the graph representing the
	// actual step of the algorithm
	public String getGraphPath(){
		if (noGraphPaths())
			return null;
		
		return graphPaths.get(position);	
	}
		
	// returns the number of line in the pseudocode
	// that matches the last graph returned
	public int getPseudoCodeLine(){
		if (noPseudocodeLines())
			return 0;
			
		int line = pseudocodeLines.get(position);
		return line;
	}
	
	// saves the graph and adds the path to the list
	protected void saveGraph(Graph g, String path){
		g.save(path);
		graphPaths.add(path);
	}

	// returns true if the list of pseudocode lines is empty
	protected boolean noPseudocodeLines(){
		return (pseudocodeLines == null) || (pseudocodeLines.isEmpty());
	}

	// returns true if the list of graph paths lines is empty
	protected boolean noGraphPaths(){
		return (graphPaths == null) || (graphPaths.isEmpty());
	}
	
	protected boolean noGraph(Graph g){
		if(g == null || g.getNodes().isEmpty())
			return true;
		return false;
	}
	
	// resets the applicator to change the graph
	// and solve the problem again
	public void reset(){
		//donde se guarda cada archivo que es un paso del algoritmo 
		graphPaths = new ArrayList<String>();
		
		//que linea del texto es la que se resalta en cada paso
		pseudocodeLines = new ArrayList<Integer>();
		
		auxText = new ArrayList<String>();
		
		position = 0;
	}
	
    public String getAuxText(){
		if(auxText.isEmpty())
			return "";
		return auxText.get(position);
    }
}
